package com.example.administrator.x2picturetransliteration.Home.Activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.Base.BaseActivity;
import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.QRCodeUtil;
import com.example.administrator.x2picturetransliteration.Util.ToastUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 生成二维码
 * Created by Administrator on 2019/8/22.
 */

public class GetQrActivity extends BaseActivity{

    @BindView(R.id.qeImg)ImageView qeImg;
    @BindView(R.id.ljText)TextView ljText;
    @BindView(R.id.okText)TextView okText;

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_getqr;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText(getIntent().getStringExtra("title"));

        LButton.setImageResource(R.mipmap.home_shape3);

        if ("生成二维码".equals(getIntent().getStringExtra("title"))){

            ljText.setVisibility(View.GONE);

            Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.mipmap.home_logo);

            Bitmap qr = QRCodeUtil.createQRImage(getIntent().getStringExtra("url"),100,100,bmp);

            qeImg.setImageBitmap(qr);

        }else {

            ljText.setText(getIntent().getStringExtra("url"));
            qeImg.setVisibility(View.GONE);
            okText.setText("复制链接");
        }

    }


    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }

    @OnClick(R.id.okText)
    public void okTextClick(){

        if ("生成二维码".equals(getIntent().getStringExtra("title"))){

            qeImg.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(qeImg.getDrawingCache());
            qeImg.setDrawingCacheEnabled(false);

            saveImageToGallery(bitmap,new Date().getTime() + "");

        }else {

            ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            cmb.setText(ljText.getText() + "");
            new ToastUtil(this).getToast(false,"复制成功").show();

        }

    }


    /** 保存图片 */
    public void saveImageToGallery(Bitmap bmp, String bitName ) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(),
                "yingtan");
        if (!appDir.exists()) {
            appDir.mkdir();
        }

        String fileName = bitName + ".jpg";
        File file = new File(appDir, fileName);

        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
            new ToastUtil(this).getToast(false,"保存成功").show();

            // 通知图库更新
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                MediaScannerConnection.scanFile(this, new String[]{file.getAbsolutePath()}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                mediaScanIntent.setData(uri);
                                sendBroadcast(mediaScanIntent);
                            }
                        });
            } else {
                String relationDir = file.getParent();
                File file1 = new File(relationDir);
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file1.getAbsoluteFile())));
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
